//
//  WSTabBarView.m
//  WeiboSpace
//
//  Created by nan4 li on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WSTabBarView.h"
@interface WSTabBarView()
-(void)configItems:(NSArray *)items;
- (void)buttonAction:(id)sender;
@end

@implementation WSTabBarView
@synthesize buttonArray;
@synthesize _delegate;
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) 
    {
        self.backgroundColor = [UIColor darkGrayColor];   
    }
    buttonArray = [[NSMutableArray alloc]init];
    
    NSArray *tabItems = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tabbarList" ofType:@"plist"]];

    [self configItems:tabItems];
    return self;
}

-(void)dealloc
{
    [focusImageView release],focusImageView = nil;
    [_delegate release],_delegate = nil;
    self.buttonArray = nil;
    //[curButton release],buttonArray = nil;
    [super dealloc];
}

-(void)configItems:(NSArray *)items
{
    //设置聚焦的image
    focusImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320/4, 49)];
    focusImageView.backgroundColor = [UIColor redColor];
    [self addSubview:focusImageView];
    
    //items
    float xleft = 0;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    for (int x=0; x<items.count; x++) {
        NSDictionary *dic = [items objectAtIndex:x];
        UIButton *button = [self generateButton:dic];
        //设置样式和区域
        button.frame = CGRectMake(xleft, 0, 320/4, 49);
        xleft += 320/4;
        [self addSubview:button];
        [buttonArray addObject:button];
    }
    [pool release];
    if([buttonArray count]>0)
        curButton = [buttonArray objectAtIndex:0];
}
-(UIButton*)generateButton:(NSDictionary*)dic
{
    UIButton *button = [[[UIButton alloc]init]autorelease];
    [button setBackgroundColor:[UIColor clearColor]];
    NSString *title = [dic objectForKey:@"title"];
	[button setTitle:title forState:UIControlStateNormal];
	[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
	//[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)buttonAction:(id)sender
{
    UIButton *selButton = (UIButton*)sender;
    if(curButton != selButton)
    {
        curButton = selButton;
        [UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:0.4];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        focusImageView.frame = selButton.frame;
        //[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:curButton cache:YES];
        
        [UIView commitAnimations];
        
    }
    
    curIndex = [buttonArray indexOfObject:selButton];
    if([_delegate respondsToSelector:@selector(tabBarItemSelected:didselectItem:)])
        [_delegate tabBarItemSelected:selButton didselectItem:curIndex];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
